﻿using NB.MGJ.Common;
using NB.MGJ.Model;
using NB.MGJ.Model.CodeManage;
using NB.MGJ.Service.Origins;
using NB.MGJ.Service.Product;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{

    /// <summary>
    /// 产品列表
    /// </summary>
    /// =======================================================================
    /// 类名：ProductController ProductController的备注：ProductController~1<br/>
    /// =======================================================================<br/>
    /// 更新履历<br/>
    /// 序号          更新日期          责任人           更新内容<br/>
    /// 001           2017年9月2日       段扬             新建<br/>
    /// =======================================================================<br/>
    public partial class ProductController
    {


        //-----------------------------------------产品批次start-------------------------------------//

        #region 产品批次视图

        public ActionResult ProductBatch()
        {
            ViewBag.User = LogOnUser;



            var sys_menuroleinfo = _ManagerService.GetSys_MenuRoleInfoByRoleIdAndMenuId(LogOnUser.RoleID, 20);

            ViewBag.sys_menuroleinfo = sys_menuroleinfo;

            return View();
        }
        public ActionResult AddBatch()
        {
            ViewData["ProductId"] = _productService.GetProductSelectList(0, LogOnUser.EnterpriseId);
            ViewData["MaterialId"] = _originsMaterialService.GetMaterialSelectList(0, LogOnUser.EnterpriseId);
            return View();
        }

        public ActionResult EditBatch(int? id)
        {
            Product_Batch p = _ProductBatchService.GetModelById((int)id);
            ViewData["ProductId"] = _productService.GetProductSelectList((int)p.ProductId, LogOnUser.EnterpriseId);
            if (p.MaterialId == null)
            {
                p.MaterialId = 0;
            }
            ViewData["MaterialId"] = _originsMaterialService.GetMaterialSelectList((int)p.MaterialId, LogOnUser.EnterpriseId);
            ViewBag.pro = p;
            return View();
        }

        #endregion

        #region 增

        public ActionResult AddProductBatch(L.EntityModel.Product_Batch productBatch)
        {
            productBatch.ViewDel = 0;
            productBatch.ChangeLoginID = LogOnUser.UserID;
            productBatch.ChangeLoginName = LogOnUser.LoginName;
            productBatch.CommonId = 0;
            productBatch.EnterpriseId = LogOnUser.EnterpriseId;

            //0 为新增  1 为绑定
            if (productBatch.IsNewMaterial == 0)
            {
                MaterialFactor mf = new MaterialFactor(LogOnUser);
                Origins_Material mModel = new Origins_Material();
                mModel.ChangeDate = DateTime.Now;
                mModel.ChangeLoginID = LogOnUser.UserID;
                mModel.ChangeLoginName = LogOnUser.LoginName;

                mModel.EnterpriseId = LogOnUser.EnterpriseId;
                mModel.Category = 0;
                mModel.ViewDel = 0;

                mModel.MaterialName = productBatch.BatchName + "默认素材";
                mModel.MaterialType = productBatch.Version;
                var mresult = mf.Create(mModel);
                productBatch.MaterialId = mresult;
            }

            var result = _ProductBatchService.AddProductBatch(productBatch);
            return Json(result);
        }

        #endregion

        #region 删

        public int DelBatchList(int id)
        {
            int result = _ProductBatchService.DelBatchService(id);
            return result;
        }

        #endregion

        #region 改

        public int UpdBatch(Product_Batch model)
        {
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.EnterpriseId = LogOnUser.EnterpriseId;
            model.ViewDel = 0;
            int rulset = _ProductBatchService.Updbatch(model);
            return rulset;

        }

        #endregion

        #region 查

        public ActionResult ProductBatchList(DetailSearch Smodel, int pageindex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageindex;
            pm.PageSize = pageSize;

            //if (string.IsNullOrEmpty(Psearch))
            //{
            //    pm.StrCondition = " ViewDel=0 and EID=" + LogOnUser.EnterpriseId;
            //}
            //else
            //{
            //    pm.StrCondition = " ViewDel=0 and  BatchName like '%" + Psearch + "%' and EID=" + LogOnUser.EnterpriseId;
            //}

            var list = _ProductBatchService.GetPageLists(ref pm, Smodel, LogOnUser);
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetProductBatchListByEID(int EID)
        {
            var result = _ProductBatchService.GetListByEID(EID);
            return Json(result,JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 导入导出

        public ActionResult ExportExecl(string key)
        {
            var model = _ProductBatchService.GetAllBatch(key);
            var _byte_batch = Excel.Exprot<Product_Batch>(model.ToList().Where(p => p.ViewDel == 0).ToList(), new string[] { "批次名称", "商品名称", "所属机构", "创建时间" }, p =>
            {
                return new object[] { 
                            p.BatchName,
                            p.ProductName,
                            p.EnterpriseName,
                            p.ChangeDate
							
				};
            }, "ProductBatch");
            return File(_byte_batch, "application/octet-stream", string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
        }

        #endregion


    }
}